<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机台编号" prop="maccode">
        <el-input
          v-model="queryParams.maccode"
          placeholder="请输入机台编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品种编号" prop="pdtcode">
        <el-input
          v-model="queryParams.pdtcode"
          placeholder="请输入品种编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轴号" prop="shaftcode">
        <el-input
          v-model="queryParams.shaftcode"
          placeholder="请输入轴号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="ordercode">
        <el-input
          v-model="queryParams.ordercode"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班次日期" prop="shiftdate">
        <el-date-picker clearable size="small"
          v-model="queryParams.shiftdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择班次日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="班次类别" prop="shifttype">
        <el-select v-model="queryParams.shifttype" placeholder="请选择班次类别" clearable size="small">
          <el-option
            v-for="dict in shifttypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否跨班次" prop="iscrossshift">
        <el-select v-model="queryParams.iscrossshift" placeholder="请选择是否跨班次" clearable size="small">
          <el-option
            v-for="dict in iscrossshiftOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上轴时间" prop="sjtime">
        <el-date-picker clearable size="small"
          v-model="queryParams.sjtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择上轴时间">
        </el-date-picker>
      </el-form-item>


      <el-form-item label="了机时间" prop="ljtime">
        <el-date-picker clearable size="small"
          v-model="queryParams.ljtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择了机时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="轴状态" prop="beamstatus">
        <el-select v-model="queryParams.beamstatus" placeholder="请选择轴状态" clearable size="small">
          <el-option
            v-for="dict in beamstatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作员" prop="opworker">
        <el-select v-model="queryParams.opworker" placeholder="请选择操作员" clearable size="small">
          <el-option
            v-for="dict in opworkerOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="整经长度" prop="curlength">
        <el-input
          v-model="queryParams.curlength"
          placeholder="请输入整经长度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设定长度" prop="setlength">
        <el-input
          v-model="queryParams.setlength"
          placeholder="请输入设定长度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="断头率" prop="breakrate">
        <el-input
          v-model="queryParams.breakrate"
          placeholder="请输入断头率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="断头数" prop="breaknum">
        <el-input
          v-model="queryParams.breaknum"
          placeholder="请输入断头数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卷绕密度" prop="density">
        <el-input
          v-model="queryParams.density"
          placeholder="请输入卷绕密度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压纱力" prop="pressurea">
        <el-input
          v-model="queryParams.pressurea"
          placeholder="请输入压纱力"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纱支" prop="yarncount">
        <el-input
          v-model="queryParams.yarncount"
          placeholder="请输入纱支"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="张力片打开时间" prop="ganopentime">
        <el-input
          v-model="queryParams.ganopentime"
          placeholder="请输入张力片打开时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="张力杆打开时间" prop="pianopentime">
        <el-input
          v-model="queryParams.pianopentime"
          placeholder="请输入张力杆打开时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="张力杆位置" prop="ganlocation">
        <el-input
          v-model="queryParams.ganlocation"
          placeholder="请输入张力杆位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总经根数" prop="warpnum">
        <el-input
          v-model="queryParams.warpnum"
          placeholder="请输入总经根数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mesana:warp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mesana:warp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mesana:warp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mesana:warp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一索引" align="center" prop="id" />
      <el-table-column label="机台编号" align="center" prop="maccode" />
      <el-table-column label="品种编号" align="center" prop="pdtcode" />
      <el-table-column label="轴号" align="center" prop="shaftcode" />
      <el-table-column label="订单编号" align="center" prop="ordercode" />
      <el-table-column label="班次日期" align="center" prop="shiftdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shiftdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="班次类别" align="center" prop="shifttype">
        <template slot-scope="scope">
          <dict-tag :options="shifttypeOptions" :value="scope.row.shifttype"/>
        </template>
      </el-table-column>
      <el-table-column label="是否跨班次" align="center" prop="iscrossshift">
        <template slot-scope="scope">
          <dict-tag :options="iscrossshiftOptions" :value="scope.row.iscrossshift"/>
        </template>
      </el-table-column>
      <el-table-column label="上轴时间" align="center" prop="sjtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="了机时间" align="center" prop="ljtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ljtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="轴状态" align="center" prop="beamstatus">
        <template slot-scope="scope">
          <dict-tag :options="beamstatusOptions" :value="scope.row.beamstatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作员" align="center" prop="opworker">
        <template slot-scope="scope">
          <dict-tag :options="opworkerOptions" :value="scope.row.opworker"/>
        </template>
      </el-table-column>
      <el-table-column label="整经长度" align="center" prop="curlength" />
      <el-table-column label="设定长度" align="center" prop="setlength" />
      <el-table-column label="断头率" align="center" prop="breakrate" />
      <el-table-column label="断头数" align="center" prop="breaknum" />
      <el-table-column label="卷绕密度" align="center" prop="density" />
      <el-table-column label="压纱力" align="center" prop="pressurea" />
      <el-table-column label="纱支" align="center" prop="yarncount" />
      <el-table-column label="张力片打开时间" align="center" prop="ganopentime" />
      <el-table-column label="张力杆打开时间" align="center" prop="pianopentime" />
      <el-table-column label="张力杆位置" align="center" prop="ganlocation" />
      <el-table-column label="总经根数" align="center" prop="warpnum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mesana:warp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mesana:warp:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改整经数据汇总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机台编号" prop="maccode">
          <el-input v-model="form.maccode" placeholder="请输入机台编号" />
        </el-form-item>
        <el-form-item label="品种编号" prop="pdtcode">
          <el-input v-model="form.pdtcode" placeholder="请输入品种编号" />
        </el-form-item>
        <el-form-item label="轴号" prop="shaftcode">
          <el-input v-model="form.shaftcode" placeholder="请输入轴号" />
        </el-form-item>
        <el-form-item label="订单编号" prop="ordercode">
          <el-input v-model="form.ordercode" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="班次日期" prop="shiftdate">
          <el-date-picker clearable size="small"
            v-model="form.shiftdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择班次日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="班次类别">
          <el-radio-group v-model="form.shifttype">
            <el-radio
              v-for="dict in shifttypeOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否跨班次">
          <el-radio-group v-model="form.iscrossshift">
            <el-radio
              v-for="dict in iscrossshiftOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上轴时间" prop="sjtime">
          <el-date-picker clearable size="small"
            v-model="form.sjtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上轴时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="了机时间" prop="ljtime">
          <el-date-picker clearable size="small"
            v-model="form.ljtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择了机时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="轴状态">
          <el-radio-group v-model="form.beamstatus">
            <el-radio
              v-for="dict in beamstatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="操作员" prop="opworker">
          <el-select v-model="form.opworker" placeholder="请选择操作员">
            <el-option
              v-for="dict in opworkerOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="整经长度" prop="curlength">
          <el-input v-model="form.curlength" placeholder="请输入整经长度" />
        </el-form-item>
        <el-form-item label="设定长度" prop="setlength">
          <el-input v-model="form.setlength" placeholder="请输入设定长度" />
        </el-form-item>
        <el-form-item label="断头率" prop="breakrate">
          <el-input v-model="form.breakrate" placeholder="请输入断头率" />
        </el-form-item>
        <el-form-item label="断头数" prop="breaknum">
          <el-input v-model="form.breaknum" placeholder="请输入断头数" />
        </el-form-item>
        <el-form-item label="卷绕密度" prop="density">
          <el-input v-model="form.density" placeholder="请输入卷绕密度" />
        </el-form-item>
        <el-form-item label="压纱力" prop="pressurea">
          <el-input v-model="form.pressurea" placeholder="请输入压纱力" />
        </el-form-item>
        <el-form-item label="纱支" prop="yarncount">
          <el-input v-model="form.yarncount" placeholder="请输入纱支" />
        </el-form-item>
        <el-form-item label="张力片打开时间" prop="ganopentime">
          <el-input v-model="form.ganopentime" placeholder="请输入张力片打开时间" />
        </el-form-item>
        <el-form-item label="张力杆打开时间" prop="pianopentime">
          <el-input v-model="form.pianopentime" placeholder="请输入张力杆打开时间" />
        </el-form-item>
        <el-form-item label="张力杆位置" prop="ganlocation">
          <el-input v-model="form.ganlocation" placeholder="请输入张力杆位置" />
        </el-form-item>
        <el-form-item label="总经根数" prop="warpnum">
          <el-input v-model="form.warpnum" placeholder="请输入总经根数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarp, getWarp, delWarp, addWarp, updateWarp, exportWarp } from "@/api/mesana/warp";

export default {
  name: "Warp",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 整经数据汇总表格数据
      warpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班次类别字典
      shifttypeOptions: [],
      // 是否跨班次字典
      iscrossshiftOptions: [],
      // 轴状态字典
      beamstatusOptions: [],
      // 操作员字典
      opworkerOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maccode: null,
        pdtcode: null,
        shaftcode: null,
        ordercode: null,
        shiftdate: null,
        shifttype: null,
        iscrossshift: null,
        sjtime: null,
        ljtime: null,
        beamstatus: null,
        opworker: null,
        curlength: null,
        setlength: null,
        breakrate: null,
        breaknum: null,
        density: null,
        pressurea: null,
        yarncount: null,
        ganopentime: null,
        pianopentime: null,
        ganlocation: null,
        warpnum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        maccode: [
          { required: true, message: "机台编号不能为空", trigger: "blur" }
        ],
        pdtcode: [
          { required: true, message: "品种编号不能为空", trigger: "blur" }
        ],
        shaftcode: [
          { required: true, message: "轴号不能为空", trigger: "blur" }
        ],
        ordercode: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dict_shiftType").then(response => {
      this.shifttypeOptions = response.data;
    });
    this.getDicts("dict_isCrossShift").then(response => {
      this.iscrossshiftOptions = response.data;
    });
    this.getDicts("dict_beamstatus").then(response => {
      this.beamstatusOptions = response.data;
    });
    this.getDicts("dict_warpworkers").then(response => {
      this.opworkerOptions = response.data;
    });
  },
  methods: {
    /** 查询整经数据汇总列表 */
    getList() {
      this.loading = true;
      listWarp(this.queryParams).then(response => {
        this.warpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        maccode: null,
        pdtcode: null,
        shaftcode: null,
        ordercode: null,
        shiftdate: null,
        shifttype: 0,
        iscrossshift: 0,
        sjtime: null,
        ljtime: null,
        beamstatus: 0,
        opworker: null,
        curlength: null,
        setlength: null,
        breakrate: null,
        breaknum: null,
        density: null,
        pressurea: null,
        yarncount: null,
        ganopentime: null,
        pianopentime: null,
        ganlocation: null,
        warpnum: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加整经数据汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWarp(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改整经数据汇总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarp(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarp(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除整经数据汇总编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWarp(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有整经数据汇总数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWarp(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
